<?php
require('setup.inc.php');

class pointsHistory {
	var $errors;
	var $drivers;
	var $totalHistory;
	var $currentPage;
	var $historyOffset = 50;
	
	function getHistory($page = 1){
		$this->getTotalRows();
		$offset = $page * $this->historyOffset - $this->historyOffset;
		if ($offset % $this->historyOffset != 0 OR $offset < 1 OR $offset >= $this->totalHistory) $offset = 0;
		$this->currentPage = ($offset + $this->historyOffset) / $this->historyOffset;
		$query = "SELECT *, date_format(timestamp, '%b %D, %Y %H:%i:%S') AS timestamp FROM t_seniority ORDER BY timestamp DESC LIMIT $offset, $this->historyOffset";
		$result = mysql_query($query);
		$this->page = $page;
		if ($result) return $result;
		else return false;
	}
	
	function getTotalRows(){
		$query = "SELECT * FROM t_seniority";
		$result = mysql_query($query);
		$this->totalHistory = mysql_num_rows($result);
	}
	
	function driverList(){
		$query = "SELECT driver_id, lastname, firstname FROM t_drivers";
		$result = mysql_query($query);
		while($data = mysql_fetch_array($result)){
			$id = $data['driver_id'];
			$fn = $data['firstname'];
			$ln = $data['lastname'];
			$this->drivers[$id] = $fn . " " . $ln;
		}
	}
	
	function showHistory($page){
		if ($page < 1) $page = 1;
		$this->driverList();
		$historyResult = $this->getHistory($page);
		if (!$historyResult) echo "<p class=\"error\">Error retrieving data. Please try again. If this error persists, please contact an administrator.";
		else {
			$i = ($page * $this->historyOffset) - $this->historyOffset + 1;
			while ($historyData = mysql_fetch_array($historyResult)){
				$driverid = $historyData['driver_id'];
				$entryby = $historyData['entry_by'];
				echo "<tr><td>" . $i . ") " . $this->drivers[$driverid] . "</td><td>" . $this->drivers[$entryby] . "</td><td align=\"center\">" . number_format($historyData['points'], 2) . "</td><td>" . $historyData['description'] . "</td><td align=\"center\">" . $historyData['timestamp'] . "</td></tr>\n";
				$i++;
			}
		}
	}
	
	function showButtons(){
		$totalPages = ceil($this->totalHistory / $this->historyOffset);
		if ($totalPages != 1){
			if ($this->totalPages != 1 AND $this->currentPage != $totalPages) echo "<a href=\"?page=" . ($this->currentPage + 1) . "\">NEXT</a> ";
			for ($i = 1; $i <= $totalPages; $i++) {
				if ($this->currentPage == $i) echo $i . " ";
				else echo "<a href=\"?page=" . $i . "\">" . $i . "</a> ";
			}
			if ($this->totalPages != 1 AND $this->currentPage != 1) echo "<a href=\"?page=" . ($this->currentPage - 1) . "\">PREV</a> ";
		}
	}
	
	function setOffset($offset){ $this->historyOffset = $offset; }
}
?>
<style type="text/css">
p, div, table, select, option, input, textarea {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
}
.error {
	color: #CC0000;
	margin-bottom: 20px;
}
</style>
<table cellpadding="2">
	<tr>
		<th width="100">Driver</th>
		<th width="100">Added By</th>
		<th>Adjustment</th>
		<th>Description</th>
		<th width="175">Date</th>
	</tr>
<?
$pointsHistory = new PointsHistory();
$pointsHistory->setOffset(30);
$pointsHistory->showHistory($_GET['page']);
?>
</table>
<p>
<?
$pointsHistory->showButtons();
?>
</p>